package com.service.impl;

import com.baseEnum.CourseType;
import com.common.convert.Convert;
import com.common.domain.Page;
import com.common.domain.Pageable;
import com.entity.SelectCourse;
import com.entity.Student;
import com.mapper.StudentMapper;
import com.pagehelper.PageHelper;
import com.service.IStudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * linjie.jin
 * Created by admin on 2016/1/7.
 */
@Service
public class StudentServiceImpl implements IStudentService {
    private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);
    @Resource
    private StudentMapper studentMapper;

    public SelectCourse find(String stuNo) {
        return studentMapper.selectCourseTable(stuNo);
    }

    /**
     *保存学生已选的课程
     * @param selectCourse
     * linjie.jin
     */
    public void save(SelectCourse selectCourse) {

        //根据学生的学号和课程号查询选课表中的条数
        Long count = studentMapper.getCourseNoCount(selectCourse.getCourseNo(), selectCourse.getStuNo());

        //如果数据库中该学生没有选该课程时
        if (count < 1) {
            List<Map> map = studentMapper.getDetailCourse(selectCourse.getId());
            selectCourse.setCourseNo(Convert.asString(map.get(0).get("course_no")));
            selectCourse.setCourseName((String) map.get(0).get("course_name"));
            selectCourse.setCredit((Integer) map.get(0).get("credit"));
            selectCourse.setCourseType(CourseType.get(Convert.asInt(map.get(0).get("course_type"))));
            selectCourse.setTeacherName((String) map.get(0).get("name"));
            selectCourse.setClassroomNo((String) map.get(0).get("classroom_no"));
            selectCourse.setPlace((String) map.get(0).get("place"));
            selectCourse.setStuNo(selectCourse.getStuNo());
            studentMapper.insert(selectCourse);
        } else {
            log.debug("该课程已选！", count);
        }
    }

    /**
     * 选课信息
     * @param pageable
     * @param courseType
     * @return
     * linjie.jin
     */
    public List<Map> findPage(Pageable pageable, CourseType courseType) {
        Page<Map> page = new Page();
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        try {
            //查询选课信息
            List<Map> list = studentMapper.selectCourse(courseType);
            Long count = studentMapper.selectNum(courseType);
            page.addAll(list);
            page.setTotal(count);
            page.setPageable(pageable);
        } catch (Exception e) {
            log.error("信息模板不存在！", e);
        }
        return page;
    }

    /**
     * 查询选课详细信息
     * @param id
     * @return
     * linjie.jin
     */
    public List<Map> getDetailCourse(Integer id) {
        return studentMapper.getDetailCourse(id);
    }

    /**
     * 查看分数
     * @return
     */
    public List<Map> selectGrade(String stuNo) {
        return studentMapper.selectGrade(stuNo);
    }

    /**
     * 根据学号查询学生信息
     * @return
     */
    public List<Map> selectAllStuInfo() {
        return studentMapper.selectAllStuInfo();
    }

    /**
     * 查询学生选修课数量
     * @param stuNo
     * @return
     */
    public Long selectCount(String stuNo,CourseType courseType) {
        return studentMapper.selectCourseCount(stuNo, courseType);
    }

    /**
     * 通过学号查询学生
     *
     * @param stuNo
     * @return
     */
    public Student getStudentByStuNo(String stuNo) {
        return studentMapper.getByStuId(stuNo);
    }

    /**
     * 更新学生
     * @param student
     */
    public void updateStudent(Student student) {
        studentMapper.updateByEntity(student);
    }
}


